package address

import (
	"shop/formdata"
	add2 "shop/internal/models/address"
	"strings"
)

func AddressAdd(add formdata.Address) (*add2.Address, error) {
	return new(add2.Address).AddressAdd(add)
}

type AddList struct {
	Token       string `json:"token"`
	Id          int    `json:"id"`
	AddressLine string `json:"addressLine"`
	Label       string `json:"label"`
	Name        string `json:"name"`
	Phone       string `json:"phone"`
	UnDefault   int    `json:"unDefault"`
}

func AddressList() ([]AddList, error) {
	// 从数据库获取原始数据
	list, err := new(add2.Address).AddressList()
	if err != nil {
		return nil, err
	}

	// 创建目标切片
	addList := make([]AddList, 0, len(list))

	// 遍历转换数据
	for _, v := range list {
		// 合并地区信息
		address := strings.Join([]string{v.Province, v.City, v.Village, v.AddressLine}, "")

		// 结构体转换
		addList = append(addList, AddList{

			Id:          int(v.ID),
			AddressLine: address,
			Label:       v.Table,
			Name:        v.Recipient,
			Phone:       v.Phone,
			UnDefault:   v.UnDefault,
		})
	}

	return addList, nil
}
